//
//  GameViewController.swift
//  FlappyBird
//
//  Created by WTFKL on 17/9/4.
//  Copyright © 2017年 WTF. All rights reserved.
//

import UIKit
import SpriteKit
import GameplayKit

class GameViewController: UIViewController {

    var titleImg : UIImageView? = nil;
    var playButton : UIButton? = nil;
    var Tscene : SKScene!;
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if let view = self.view as! SKView? {
            // Load the SKScene from 'GameScene.sks'
            if let scene = SKScene(fileNamed: "GameScene") {
                // Set the scale mode to scale to fit the window
                scene.scaleMode = .aspectFill
               
                // Present the scene
                view.presentScene(scene)
                scene.childNode(withName: "moving")?.speed = 0 ;
                scene.childNode(withName: "bird")?.physicsBody?.isDynamic = false;
                Tscene = scene;
            }
            
            view.ignoresSiblingOrder = true
            
            view.showsFPS = true
            view.showsNodeCount = true
        }
        
        setupUI();
    }

    
    func setupUI(){
        titleImg = UIImageView(image: #imageLiteral(resourceName: "title"));
        titleImg?.center = CGPoint(x: view.center.x, y: view.center.y-100);
        self.view.addSubview(titleImg!);
        
        playButton = UIButton();
        playButton?.frame.size = CGSize(width: 100, height: 100);
        playButton?.setImage(#imageLiteral(resourceName: "button_resume"), for: .normal);
        playButton?.center = CGPoint(x:view.center.x, y:view.center.y);
        playButton?.addTarget(self, action: #selector(startGame), for: .touchUpInside);
        self.view.addSubview(playButton!);
        
        
        
    }
    
    
    func startGame(){
        titleImg?.isHidden = true;
        playButton?.isHidden  = true;
        
        Tscene.childNode(withName: "moving")?.speed = 1 ;
        Tscene.childNode(withName: "bird")?.physicsBody?.isDynamic = true ;
    }
    
    
    
    override var shouldAutorotate: Bool {
        return true
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        if UIDevice.current.userInterfaceIdiom == .phone {
            return .allButUpsideDown
        } else {
            return .all
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Release any cached data, images, etc that aren't in use.
    }

    override var prefersStatusBarHidden: Bool {
        return true
    }
}
